package com.deductive.web.disposition.special;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deductive.common.core.controller.BaseController;
import com.deductive.common.core.domain.AjaxResult;
import com.deductive.domain.special.SpecialTableColumn;
import com.deductive.mapper.PgDBMapper;
import com.deductive.service.special.SpecialTableColumnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * @author 王浩
 * @since 2024-11-06 14:12:51
 */
@RestController
@RequestMapping("desdTableColumn")
@Api(tags = "专题数据字段管理")
public class SpecialTableColumnController extends BaseController {


    @Resource
    private SpecialTableColumnService desdTableColumnService;

    @Autowired
    private PgDBMapper pgDBMapper;


    /**
     * 分页查询所有数据
     *
     * @param page            分页对象
     * @param desdTableColumn 查询实体
     * @return 所有数据
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public AjaxResult selectPage(Page<SpecialTableColumn> page, SpecialTableColumn desdTableColumn) {
        LambdaQueryWrapper<SpecialTableColumn> wrapper = new LambdaQueryWrapper<>(desdTableColumn);
        wrapper.like(desdTableColumn.getTableName() != null, SpecialTableColumn::getTableName, desdTableColumn.getTableName());
        return AjaxResult.success(this.desdTableColumnService.page(page, wrapper));
    }


    @GetMapping("/list")
    @ApiOperation("条件查询")
    public AjaxResult selectAll(SpecialTableColumn desdTableColumn) {
        LambdaQueryWrapper<SpecialTableColumn> wrapper = new LambdaQueryWrapper<>(desdTableColumn);
        wrapper.like(desdTableColumn.getTableName() != null, SpecialTableColumn::getTableName, desdTableColumn.getTableName());
        List<SpecialTableColumn> list = this.desdTableColumnService.list(wrapper);
        return AjaxResult.success(list);
    }

    @GetMapping("/listTableName")
    @ApiOperation("查询所有表名")
    public AjaxResult listTableName() {
        LambdaQueryWrapper<SpecialTableColumn> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(SpecialTableColumn::getTableName).groupBy(SpecialTableColumn::getTableName);
        List<SpecialTableColumn> list = this.desdTableColumnService.list(wrapper);
        return AjaxResult.success(list);
    }

    @PostMapping("/insertColumn")
    @ApiOperation("添加到字段表")
    public AjaxResult insertColumn(String tableName) {
        desdTableColumnService.insertColumn(tableName);
        return AjaxResult.success();
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("根据Id查询")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return AjaxResult.success(this.desdTableColumnService.getById(id));
    }

    /**
     * 新增数据
     *
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("添加字段数据(同时将指定表中的大写字段转为小写)")
    public AjaxResult insert() {
        List<JSONObject> tableNames = pgDBMapper.getTableName();
        for (JSONObject tableName : tableNames) {
            String tablename = tableName.getString("tablename");
            if (tablename.contains("special") &&
                    !tablename.equals("special_table_column")
                    && !tablename.equals("special_data_tree")
                    && !tablename.equals("special_data")
            ) {
                List<JSONObject> columns = pgDBMapper.getColumnsContainsComment(tablename);
                for (JSONObject column : columns) {
                    String columnName = StrUtil.toCamelCase(column.getString("column_name")) ;
                    String columnComment = column.getString("column_comment");
                    SpecialTableColumn specialTableColumn = new SpecialTableColumn();
                    specialTableColumn.setColumnName(columnName);
                    specialTableColumn.setColumnRemark(columnComment);
                    specialTableColumn.setTableName(tablename);
                    desdTableColumnService.save(specialTableColumn);
                }
            }
        }
        return AjaxResult.success();
    }


    /**
     * 修改数据
     *
     * @param desdTableColumn 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation("修改数据字段")
    public AjaxResult update(@RequestBody SpecialTableColumn desdTableColumn) {
        return AjaxResult.success(this.desdTableColumnService.updateById(desdTableColumn));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation("删除数据字段")
    public AjaxResult delete(@RequestParam("idList") List<Long> idList) {
        boolean b = this.desdTableColumnService.removeByIds(idList);
        return AjaxResult.success(b);
    }

}

